#include "RenderPch.h"

#include "ShaderManager.h"

#include "ShaderDiffuse.h"
#include "ShaderToon.h"
#include "ShaderToon2.h"

ShaderManager::ShaderManager(void)
{	
}

ShaderManager::~ShaderManager(void)
{
	for( int cnt = 0; cnt < MAX_SHADER; cnt++ )
	{
		SAFE_DELETE(m_shaderList[cnt]);
	}

	std::map<std::string, LPD3DXEFFECT>::iterator it = m_effectList.begin();
	for( ; it != m_effectList.end(); it++ )
	{
		SAFE_RELEASE(it->second);
	}
}

void ShaderManager::InitShader()
{
	m_shaderList[DIFFUSE_SHADER]	= new ShaderDiffuse();
	m_shaderList[TOON_SHADER]		= new ShaderToon();
	m_shaderList[TOON2_SHADER]		= new ShaderToon2();
}

ShaderType ShaderManager::GetShaderTypeByString( std::string type )
{
	if( type == "diffuse" )
	{
		return DIFFUSE_SHADER;
	}
	else if( type == "toon" )
	{
		return TOON_SHADER;
	}
	else if( type == "toon2" )
	{
		return TOON2_SHADER;
	}

	return MAX_SHADER;
}

LPD3DXEFFECT ShaderManager::GetD3DEffect( std::string fileName )
{
	std::map<std::string, LPD3DXEFFECT>::iterator it = m_effectList.find(fileName);
	if( it != m_effectList.end() )
	{
		return it->second;
	}

	std::string filePath = "Shader/" + fileName;

	LPD3DXEFFECT effect = NULL;
	if( FAILED( D3DXCreateEffectFromFile( RenderFacade::GetInstance()->GetDxDevice(), filePath.c_str(), NULL, NULL, 0, NULL, &effect, NULL ) ) )
	{
		return NULL;
	}

	m_effectList[fileName] = effect;

	return effect;
}
